/*
 * 纱嘴交换
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "yarn_exchange.h"
#include "com.h"
#include "keypane.h"
#include "info.h"
#include "run.h"

#define YARN_EXCHANGE_CAPTION_TOP        (CY_TITLE4_HEIGHT + CX_EDGE_LEFT)
#define YARN_EXCHANGE_CAPTION_WIDTH      (60)
#define YARN_EXCHANGE_CAPTION_HEIGHT     (30)
#define YARN_EXCHANGE_CAPTION_LEFT       (CX_EDGE_LEFT)
#define YARN_EXCHANGE_ITEM_WIDTH         (32)
/*
 *
 */
static void CreateYarnExchangeInterface(HWND hWnd)
{
    int i = 0;
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);

    CreateWindow(MH_LABEL_NAME, MW_YARN_ONE_CAPTION, WS_CHILD | WS_VISIBLE,
            IDC_STATIC, YARN_EXCHANGE_CAPTION_LEFT, YARN_EXCHANGE_CAPTION_TOP
                    + YARN_EXCHANGE_CAPTION_HEIGHT,
            YARN_EXCHANGE_CAPTION_WIDTH, YARN_EXCHANGE_CAPTION_HEIGHT, hWnd,
            MH_LABEL_COLOR_1GROUP);
    CreateWindow(MH_LABEL_NAME, MW_WIDE_YARN_CAPTION, WS_CHILD | WS_VISIBLE,
            IDC_STATIC, YARN_EXCHANGE_CAPTION_LEFT, YARN_EXCHANGE_CAPTION_TOP
                    + YARN_EXCHANGE_CAPTION_HEIGHT * 2,
            YARN_EXCHANGE_CAPTION_WIDTH, YARN_EXCHANGE_CAPTION_HEIGHT, hWnd,
            MH_LABEL_COLOR_1GROUP);
    if(1)
    {
        char *strNum[16] = { "1", "2", "3", "4", "5", "6", "7", "8", "11",
                "12", "13", "14", "15", "16", "17", "18" };
        char sc[32] = { 0 };
        uchar used_yarn[16] = { 0 };
        int id_collor = 0;
        GetSourceYarnDrt(g_knit_data, used_yarn, 1);
        for(i = 0; i < 16; ++i)
        {
            uchar uc = GetExchangedYarn(g_knit_data, 0, i);
            if(used_yarn[i])
            {
                id_collor = MH_LABEL_COLOR_3GROUP;
                if(uc == 0)
                {
                    if(i >7)
                    {
                        uc  = i + 3;
                    }
                    else
                    {
                        uc  = i + 1;
                    }
                    SetExchangedYarn(g_knit_data, 0, i, uc);
                }
            }
            else
            {
                id_collor = MH_LABEL_COLOR_1GROUP;
                SetExchangedYarn(g_knit_data, 0, i, 0);
                SetExchangedYarn(g_knit_data, 1, i, 0);
            }
            HWND hTmp = CreateWindow(MH_LABEL_NAME, strNum[i], WS_CHILD | WS_VISIBLE,
                    IDC_STATIC, YARN_EXCHANGE_CAPTION_LEFT
                            + YARN_EXCHANGE_CAPTION_WIDTH
                            + YARN_EXCHANGE_ITEM_WIDTH * i,
                    YARN_EXCHANGE_CAPTION_TOP, YARN_EXCHANGE_ITEM_WIDTH,
                    YARN_EXCHANGE_CAPTION_HEIGHT, hWnd, id_collor);

            sprintf(sc, "%u", uc);
            hTmp = CreateWindow(MH_EDIT_NAME, sc, WS_CHILD | BS_CENTER | WS_VISIBLE,
                    IDC_EDIT1 + i, YARN_EXCHANGE_CAPTION_LEFT
                            + YARN_EXCHANGE_CAPTION_WIDTH
                            + YARN_EXCHANGE_ITEM_WIDTH * i,
                    YARN_EXCHANGE_CAPTION_TOP + YARN_EXCHANGE_CAPTION_HEIGHT,
                    YARN_EXCHANGE_ITEM_WIDTH, YARN_EXCHANGE_CAPTION_HEIGHT,
                    hWnd, 0);
            if(!used_yarn[i])
            {
                EnableWindow(hTmp, FALSE);
                ShowWindow(hTmp, SW_HIDE);
            }
            uc = GetExchangedYarn(g_knit_data, 1, i);
            sprintf(sc, "%u", uc);
            hTmp = CreateWindow(MH_EDIT_NAME, sc, WS_CHILD | BS_CENTER | WS_VISIBLE,
                    IDC_EDIT1 + 16 + i, YARN_EXCHANGE_CAPTION_LEFT
                            + YARN_EXCHANGE_CAPTION_WIDTH
                            + YARN_EXCHANGE_ITEM_WIDTH * i,
                    YARN_EXCHANGE_CAPTION_TOP + YARN_EXCHANGE_CAPTION_HEIGHT
                            * 2, YARN_EXCHANGE_ITEM_WIDTH,
                    YARN_EXCHANGE_CAPTION_HEIGHT, hWnd, 0);
            if(!used_yarn[i])
            {
               EnableWindow(hTmp, FALSE);
               ShowWindow(hTmp, SW_HIDE);
            }
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    uchar n = atoi(sc);
    int sect = nID - IDC_EDIT1;
    if(sect < 16)
    {
        SetExchangedYarn(g_knit_data, 0, sect & 0x0F, n);
    }
    else
    {
        SetExchangedYarn(g_knit_data, 1, sect & 0x0F, n);
    }
    data.value = TRUE;
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 18, atoi(GetWindowCaption(hTmp)));
    }
    else if(nID == ID_ESC)
    {
        ushort used = 0;
        int i = 0;
        HWND hTmp = -1;
        int yarn = 0;
        for(i = 0; i < 32; ++i)
        {
            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + i);
            const char *sc = GetWindowCaption(hTmp);
            yarn = atoi(sc);
            if(yarn > 0 && yarn < 17)
            {
                if(used & (1 << (yarn - 1)))
                {
                    break;
                }
                else
                {
                    used |= (1 << (yarn - 1));
                }
            }
        }
        if(i < 32)
        {
            char sc[128] = {0};
            sprintf(sc, GetString(584), yarn);
            AfxInfoBox(hWnd, NULL, sc);
            return;
        }
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.value)
        {
            char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR)
                    + sizeof(CONF_FILE_SUFFIX)] = { 0 };
            strcat(sc, KNIT_FILE_DIR);
            strcat(sc, GetKnitFileName(g_knit_data));
            strcat(sc, CONF_FILE_SUFFIX);
            int ret = WriteConfigureFile(sc, g_knit_data);
            if(ret)
            {
                printf("failed to write conf file %s: %d\n", sc, ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
                SetWindowAdditionalData(hWnd, 0);
                AfxInfoBox(hWnd, NULL, GetString(488));
                FlushRunWndFile(g_hwnd_run);
            }
            SendExchangedYarn(g_knit_data);
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }

}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            OnBtnCmd(hWnd, nID);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if(((nID - IDC_EDIT1) & 0x0F) < 0x0F)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if((nID - IDC_EDIT1) & 0x0F)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 16 && nID < IDC_EDIT1 + 32)
        {
            nID -= 16;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 16)
        {
            nID += 16;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int YarnExchangeWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case MSG_CREATE:
        {
            CreateYarnExchangeInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*) lParam, &g_bmp[BMP_YARN_CHECK]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateYarnExchangeWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_YARN_CHECK_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHECK_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_YARN_EXCHANGE_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = YarnExchangeWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHECK_BMP;
    CreateInfo.by = y + CY_YARN_CHECK_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
